package sanshui.system.trade.service;

import com.lark.oapi.service.hire.v1.model.TradeDetail;
import org.springframework.stereotype.Service;
import sanshui.system.trade.common.CommonResponse;
import sanshui.system.trade.common.TradeType;
import sanshui.system.trade.db.entity.TradeTargetCode;
import sanshui.system.trade.db.entity.TradeTargetCodeExample;
import sanshui.system.trade.db.mapper.TradeTargetCodeMapper;
import sanshui.system.trade.util.CommonTimeUtil;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TradeTargetCodeServiceImpl implements ITradeTargetCodeService{

    @Resource
    TradeTargetCodeMapper targetCodeMapper;

    @Override
    public void insert(String tsCode) {
        TradeTargetCode tradeTargetCode = new TradeTargetCode();
        tradeTargetCode.setTsCode(tsCode);
        tradeTargetCode.setAddTime(CommonTimeUtil.getNowTime16());
        tradeTargetCode.setDelFlg(TradeType.DelFlg.EXIT.getCode());
        targetCodeMapper.insert(tradeTargetCode);
    }

    @Override
    public void insert(List<String> tsCode, Long workerId) {
        for (String code : tsCode) {
            TradeTargetCode tradeTargetCode = new TradeTargetCode();
            tradeTargetCode.setTsCode(code.toUpperCase());
            tradeTargetCode.setAddTime(CommonTimeUtil.getNowTime16());
            tradeTargetCode.setDelFlg(TradeType.DelFlg.EXIT.getCode());
            tradeTargetCode.setTargetWorkId(workerId);
            targetCodeMapper.insert(tradeTargetCode);
        }
    }

    @Override
    public void insertIndex(String index) {

    }

    @Override
    public void update(TradeTargetCode tradeTargetCode) {
        TradeTargetCodeExample example = new TradeTargetCodeExample();
        TradeTargetCodeExample.Criteria criteria = example.createCriteria();
        criteria.andTsCodeEqualTo(tradeTargetCode.getTsCode());
        targetCodeMapper.updateByExampleSelective(tradeTargetCode, example);
    }

    @Override
    public List<TradeTargetCode> findTargetCodesByWorkerId(Long workerId) {
        TradeTargetCodeExample example = new TradeTargetCodeExample();
        TradeTargetCodeExample.Criteria criteria = example.createCriteria();
        criteria.andTargetWorkIdEqualTo(workerId);
        criteria.andDelFlgEqualTo(TradeType.DelFlg.EXIT.getCode());
        return targetCodeMapper.selectByExample(example);
    }

    @Override
    public TradeTargetCode findTargetCodeInList(String targetCode, List<TradeTargetCode> codeList) {
        for (TradeTargetCode tradeTargetCode : codeList) {
            if (targetCode.equals(tradeTargetCode.getTsCode())){
                return tradeTargetCode;
            }
        }
        return null;
    }
}
